//
//  CustmAlertView.m
//  JUSQ
//
//  Created by admin on 16/3/25.
//  Copyright (c) 2016年 haoniu. All rights reserved.
//

#import "CustmAlertView.h"

@implementation CustmAlertView{
}

static CustmAlertView *alertView;


+(instancetype) shareInstance;{
    if(!alertView){
        alertView=[[CustmAlertView alloc] init];
        [alertView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.35]];//colorWithWhite:0 alpha:1
        //        [commPopupwindow.layer setShadowOffset:CGSizeMake(0, 4)];
        //        [commPopupwindow.layer setShadowRadius:4];
        //        [commPopupwindow.layer setShadowOpacity:0.8];
        //        [commPopupwindow.layer setShadowColor:[UIColor blackColor].CGColor];
    }else{
        for (UIView *view in alertView.subviews) {
            [view removeFromSuperview];
        }
    }
    return alertView;
    
}

+(instancetype) ShowContentView:(NSString *)title withDetail:(NSString *)detail withDelegate:(id<CustmAlertViewDelegate>)delegate withShowType:(ENUM_CustmAlertView)show{
    //初始化
    [CustmAlertView shareInstance];
    
    //添加背景
    [alertView setFrame:[UIScreen mainScreen].bounds];
    alertView.title = [NSMutableString stringWithFormat:@"%@",title==nil?@"":title];
    alertView.detail = [NSMutableString stringWithFormat:@"%@",detail==nil?@"":detail];
    alertView.custmAlertViewDelegate = delegate;
    alertView.showType = show;
    if (alertView.showType==0) {
        [alertView showLableView];
    }else{
        [alertView showTextView];
    }
    return alertView;
}

-(void)showLableView{
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(20, self.frame.size.height/2-80, self.frame.size.width-40, 160)];
    [_contentView setBackgroundColor:[UIColor whiteColor]];
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _contentView.frame.size.width, 55)];
    [titleLab setText:_title];
    [titleLab setFont:[UIFont boldSystemFontOfSize:18]];
    [titleLab setTextAlignment:NSTextAlignmentCenter];
    [_contentView addSubview:titleLab];
    
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, _contentView.frame.size.width, 2)];
    [line setBackgroundColor:[AppConfig colorWithHexString:@"#ff9a57"]];
    [_contentView addSubview:line];
    
    _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 59, _contentView.frame.size.width-20, 45)];
    [_label setTextColor:[AppConfig colorWithHexString:@"#454545"]];
    [_label setFont:[UIFont systemFontOfSize:16]];
    [_label setNumberOfLines:0];
    [_label setTextAlignment:NSTextAlignmentCenter];
    [_label setText:_detail];
    [_contentView addSubview:_label];
    
    UILabel * hengLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 114.5, _contentView.frame.size.width, 0.5)];
    [hengLine setBackgroundColor:self.backgroundColor];
    [_contentView addSubview:hengLine];
    
    UIButton * qdbtn = [[UIButton alloc]initWithFrame:CGRectMake(_contentView.frame.size.width/2, 115, _contentView.frame.size.width/2, 45)];
    [qdbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qdbtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [qdbtn setTitleColor:[AppConfig colorWithHexString:@"#0088ff"] forState:UIControlStateNormal];
    [qdbtn addTarget:self action:@selector(hideByAnimaDefaultByReturn) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:qdbtn];
    
    UILabel * shuLine = [[UILabel alloc]initWithFrame:CGRectMake(_contentView.frame.size.width/2, 115, 0.5,45)];
    [shuLine setBackgroundColor:self.backgroundColor];
    [_contentView addSubview:shuLine];
    
    [_contentView.layer setMasksToBounds:YES];
    [_contentView.layer setCornerRadius:4];
    [self addSubview:_contentView];
}
-(void)showTextView{
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(20, self.frame.size.height/2-80, self.frame.size.width-40, 160)];
    [_contentView setBackgroundColor:[UIColor whiteColor]];
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _contentView.frame.size.width, 55)];
    [titleLab setText:_title];
    [titleLab setFont:[UIFont boldSystemFontOfSize:18]];
    [titleLab setTextAlignment:NSTextAlignmentCenter];
    [_contentView addSubview:titleLab];
    
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, _contentView.frame.size.width, 2)];
    [line setBackgroundColor:[AppConfig colorWithHexString:@"#ff9a57"]];
    [_contentView addSubview:line];
    
    _tv = [[UITextField alloc]initWithFrame:CGRectMake(10, 59, _contentView.frame.size.width-20, 45)];
    [_tv setPlaceholder:_detail];
    [_tv setFont:[UIFont systemFontOfSize:16]];
    _tv.borderStyle = UITextBorderStyleRoundedRect;
    [_tv.layer setBorderColor:[[AppConfig colorWithHexString:@"#ff9a57"] CGColor]];
    _tv.delegate = self;
    [_contentView addSubview:_tv];
    
    UILabel * hengLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 114.5, _contentView.frame.size.width, 0.5)];
    [hengLine setBackgroundColor:self.backgroundColor];
    [_contentView addSubview:hengLine];
    
    UIButton * qxbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 115, _contentView.frame.size.width/2, 45)];
    [qxbtn setTitle:@"取消" forState:UIControlStateNormal];
    [qxbtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [qxbtn setTitleColor:[AppConfig colorWithHexString:@"#0088ff"] forState:UIControlStateNormal];
    [qxbtn addTarget:self action:@selector(hideByAnimaDefault) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:qxbtn];
    
    UIButton * qdbtn = [[UIButton alloc]initWithFrame:CGRectMake(_contentView.frame.size.width/2, 115, _contentView.frame.size.width/2, 45)];
    [qdbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qdbtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [qdbtn setTitleColor:[AppConfig colorWithHexString:@"#0088ff"] forState:UIControlStateNormal];
    [qdbtn addTarget:self action:@selector(hideByAnimaDefaultByReturn) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:qdbtn];
    
    UILabel * shuLine = [[UILabel alloc]initWithFrame:CGRectMake(_contentView.frame.size.width/2, 115, 0.5,45)];
    [shuLine setBackgroundColor:self.backgroundColor];
    [_contentView addSubview:shuLine];
    
    [_contentView.layer setMasksToBounds:YES];
    [_contentView.layer setCornerRadius:4];
    [self addSubview:_contentView];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    int orgy = (self.frame.size.height-216)/2-80;
    [UIView animateWithDuration:0.5 animations:^(void){
        [_contentView setFrame:CGRectMake(20, orgy, self.frame.size.width-40, 160)];
    }];
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^(void){
        [_contentView setFrame:CGRectMake(20, self.frame.size.height/2-80, self.frame.size.width-40, 160)];
    }];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    if ([touch view]==self) {
        [self hideByAnimaDefault];
    }
}

-(void)hideByAnimaDefault{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.isShown=false;
        [self removeFromSuperview];
    }];
}
-(void)hideByAnimaDefaultByReturn{
    if (_showType==1) {
        [_custmAlertViewDelegate CustmAlertView:self withButton:1 withText:_tv.text];
        [_tv setText:@""];
    }
    [self hideByAnimaDefault];
}
-(void)showByAnimaDefault{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.isShown=YES;
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    }];
}

@end
